#include <vector>
#include <iostream>

// 定义Fragment结构体
struct Fragment {
    int length;
    int offset;
};

std::vector<Fragment> fragmentPacket(int packetLength, const std::vector<int>& pathMTUs) {
    std::vector<Fragment> fragments;
    int headerLength = 20; // IP报头固定占20字节
    int totalLength = packetLength + headerLength;
    int offset = 0;

    for (size_t i = 0; i < pathMTUs.size(); ++i) {
        int mtu = pathMTUs[i];
        int dataLength = mtu - headerLength;
        while (totalLength > mtu) {
            Fragment frag;
            frag.length = mtu;
            frag.offset = offset;
            fragments.push_back(frag);
            totalLength -= dataLength;
            offset += dataLength;
        }
        if (totalLength > 0) {
            Fragment frag;
            frag.length = totalLength;
            frag.offset = offset;
            fragments.push_back(frag);
            break;
        }
    }

    return fragments;
}

int main() {
    // 示例使用
    int packetLength = 24576; // 报文长度，示例为24KB
    std::vector<int> pathMTUs = {4325, 2346, 1500, 4464, 2346}; // 传输路径上各网络的MTU
    std::vector<Fragment> result = fragmentPacket(packetLength, pathMTUs);

    for (const auto& frag : result) {
        std::cout << "Fragment Length: " << frag.length << " bytes, Offset: " << frag.offset << " bytes" << std::endl;
    }

    return 0;
}